package com.csi.service.impl;

import cn.hutool.extra.pinyin.PinyinUtil;
import com.csi.domain.*;
import com.csi.mapper.AuditMapper;
import com.csi.service.AuditService;
import com.csi.utils.MD5Utils;
import com.csi.utils.ReturnResults;
import com.csi.utils.SMS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

@Service
public class AuditServiceImpl implements AuditService {


    @Autowired
    private AuditMapper auditMapper;


//
//    @Autowired
//    private RedisTemplate<String,Object> redisTemplate;
//    private static final String KEY_PREFIX = "audit:";

    /**
     * 更新审核信息
     * @param audit 审核对象
     * @return 执行结果
     */
    @Override
    public int updateAuditHotel(Audit audit) {

        int updated = auditMapper.update(audit);
        Hotel hotel = auditMapper.selectHotel(audit.getId());
        if (ReturnResults.PASS_AUDIT_STATUS.equals(audit.getAuditStatus())){
            //发送的链接地址 -- 短信发送
            String url="?flag=common&&hotelId="+hotel.getId()+"&&userId="+audit.getAuditId();
            String phone = hotel.getHotelPhone();
            try {
                SMS.userInfo(phone, String.valueOf("flagcommonhotelId"+hotel.getId()),String.valueOf("userId"+audit.getAuditId()));
            } catch (Exception e) {
                return 0;
            }
        }

        return updated;
    }

//    /**
//     * 定时取消
//     */
//    @Scheduled(fixedDelay = 172800000) //每五秒执行一下
//    public void scheduledCancelHotel(){
//        long hotelId = (long) redisTemplate.opsForValue().get(KEY_PREFIX+"scheduleHotel");
//        String auditStatus = auditMapper.selectStatus(hotelId);
//        if (ReturnResults.OUT_AUDIT_STATUS.equals(auditStatus)){
//            auditMapper.deleteHotel(hotelId);
//        }
//    }

    /**
     * 更新加盟审核信息
     * @param audit
     * @return
     */
    @Transactional
    @Override
    public int updateAuditFranchise(Audit audit)  {
        int update = auditMapper.update(audit);
        if (ReturnResults.PASS_AUDIT_STATUS.equals(audit.getAuditStatus())){
            //更新酒店id，同时启动定时器任务
            Hotel hotel = auditMapper.selectHotel4Franchise(audit.getId());
            HotelFranchise hotelFranchise = new HotelFranchise();
            hotelFranchise.setId(audit.getHotelFranchiseId());
            hotelFranchise.setEffectTime(new Timestamp(System.currentTimeMillis()));
            Role role = new Role();
            role.setHotelId(hotel.getId());
            auditMapper.insertRole(role);

            Employee employee = new Employee();
            employee.setHotelId(hotel.getId());
            //发送的用户名
            String username = PinyinUtil.getPinyin(hotel.getHotelContext()).replace(" ","");
            //发送的密码
            String password = String.valueOf(ThreadLocalRandom.current().nextInt(100000, 1000000)); // 100000 到 999999 之间
            //发送用户名密码，短信
            try {
                SMS.userInfo(hotelFranchise.getHotelLegalContext(),username,password);
            } catch (Exception e) {
                return 0;
            }

            employee.setUsername(username);
            employee.setPassword(MD5Utils.getMD5String(password));
            employee.setEntryTime(new Timestamp(System.currentTimeMillis()));
            employee.setCreateId(0);
            employee.setRoleId(role.getId());
            employee.setName(hotel.getHotelContext());
            employee.setHotelId(hotel.getId());
            auditMapper.generateEmployee(employee);
        }
        return update;
    }




    /**
     * 更新房间审核状态
     * @param audit
     * @return
     */
    @Override
    public int updateAuditRoomType(Audit audit) {
        int update = auditMapper.update(audit);
//        if (ReturnResults.OUT_AUDIT_STATUS.equals(audit.getAuditStatus())){
//            //更新酒店id，同时启动定时器任务
//            redisTemplate.opsForValue().set(KEY_PREFIX+ "scheduleRoomType",audit.getRoomtypeId());
//            scheduledCancelRoomType();
//        }
        return update;
    }

//    /**
//     * 定时取消
//     */
//    @Scheduled(fixedDelay = 172800000) //每五秒执行一下
//    @Transactional
//    public void scheduledCancelRoomType(){
//        long roomTypeId = (long) redisTemplate.opsForValue().get(KEY_PREFIX+"scheduleRoomType");
//        String auditStatus = auditMapper.selectRoomTypeStatus(roomTypeId);
//        if (ReturnResults.OUT_AUDIT_STATUS.equals(auditStatus)){
//            auditMapper.deleteRoomType(roomTypeId);
//            auditMapper.deleteRooms(roomTypeId);
//            auditMapper.deleteRoomTypeFacility(roomTypeId);
//        }
//    }



    /**
     * 查询所有酒店审核
     * @param auditStatus
     * @return
     */
    @Override
    public Map<String, Object> findAuditHotel(String auditStatus) {
        return auditMapper.findAuditHotel(auditStatus);
    }

    /**
     * 根据酒店id查询酒店审核信息
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> findAuditHotelId(long id) {
        return auditMapper.findAuditHotelId(id);
    }

    /**
     * 查询酒店加盟审核信息
     * @param auditStatus
     * @return
     */
    @Override
    public Map<String, Object> findAuditFranchise(String auditStatus) {
        return auditMapper.findAuditFranchise(auditStatus);
    }

    @Override
    public Map<String, Object> findAuditFranchiseId(long franchiseId) {
        return auditMapper.findAuditFranchiseId(franchiseId);
    }

    @Override
    public Map<String, Object> findRoomType(String auditStatus, int peopleNum) {
        return auditMapper.findRoomType(auditStatus,peopleNum);
    }

    @Override
    public Map<String, Object> findRoomTypeId(long roomTypeId) {
        return auditMapper.findRoomTypeId(roomTypeId);
    }


}
